﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace RescueRobotController.Panels
{
    public partial class ImpactWarningPanel : DockContent
    {

        private ConcurrentQueue<ATCmd> cmdQueue;
        private Thread changeColorThread;
        private bool changeColorThreadRunning = true;

        // 下面的内容是每个面板必须要有的
        // *********** BEGIN ************* //
        // “发送AT指令”事件
        public delegate void ExistATCmdToSendHandler(ATCmd cmd);
        public event ExistATCmdToSendHandler SendATCmd;

        // 处理AT指令
        public void HandleATCmd(ATCmd cmd)
        {
            cmdQueue.Enqueue(cmd);
        }
        // *********** END ************* //

        private void ChangeColorByATCmd(ATCmd cmd)
        {
            string target;
            string value;
            float floatValue;
            Color color;

            Console.WriteLine("color");

            target = cmd.target;
            value = cmd.value;
            floatValue = float.Parse(value);
            color = Float2Color(floatValue);

            switch (target)
            {
                case "CHASSIS_WAVE1":
                    label_F.BackColor = color;
                    break;
                case "CHASSIS_WAVE2":
                    label_RF.BackColor = color;
                    break;
                case "CHASSIS_WAVE3":
                    label_R.BackColor = color;
                    break;
                case "CHASSIS_WAVE4":
                    label_RB.BackColor = color;
                    break;
                case "CHASSIS_WAVE5":
                    label_B.BackColor = color;
                    break;
                case "CHASSIS_WAVE6":
                    label_LB.BackColor = color;
                    break;
                case "CHASSIS_WAVE7":
                    label_L.BackColor = color;
                    break;
                case "CHASSIS_WAVE8":
                    label_LF.BackColor = color;
                    break;
            }
        }

        private Color Float2Color(float value)
        {
            if (value > 20) return Color.Gray;
            else if (value <= 20 && value > 15) return Color.Green;
            else if (value <= 15 && value > 10) return Color.Orange;
            else if (value <= 10) return Color.Red;
            else return Color.Red;
        }

        public ImpactWarningPanel()
        {
            InitializeComponent();
            cmdQueue = new ConcurrentQueue<ATCmd>();
            changeColorThread = new Thread(ChangeColorThread);
            
        }

        private void ChangeColorThread()
        {
            while (true)
            {
                if (!changeColorThreadRunning)
                {
                    break;
                }
                if (!cmdQueue.IsEmpty)
                {
                    ATCmd cmd;
                    cmdQueue.TryDequeue(out cmd);
                    Console.WriteLine("{0} {1} {2}", cmd.type, cmd.target, cmd.value);
                    ChangeColorByATCmd(cmd);
                }
            }
        }

        private void ImpactWarningPanel_Load(object sender, EventArgs e)
        {
            changeColorThread.Start();

        }

        private void ImpactWarningPanel_FormClosed(object sender, FormClosedEventArgs e)
        {
            changeColorThreadRunning = false;
        }

        private void ImpactWarningPanel_FormClosing(object sender, FormClosingEventArgs e)
        {
            changeColorThreadRunning = false;
        }
    }
}
